package com.fyzk.activity.fragment;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.fyzk.activity.main.RealtimeScoreActivity;
import com.fyzk.activity.main.ShishibifenActivity;
import com.fyzk.base.IBaseActivity;
import com.fyzk.utils.JsonBuilder;
import com.fyzk.utils.glide.GlideRoundTransform;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.text.Layout;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.*;
import com.fyzk.activity.ForumActivity;
import com.fyzk.activity.app.App;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.dialog.AppDialog;
import com.fyzk.activity.lottery.basketball.JCLQFragmentActivityI;
import com.fyzk.activity.lottery.gaopin.AnHuiK3Activity;
import com.fyzk.activity.lottery.gaopin.GD11x5Activity;
import com.fyzk.activity.lottery.gaopin.GDKLSFActivity;
import com.fyzk.activity.lottery.gaopin.GXK3Activity;
import com.fyzk.activity.lottery.gaopin.JLK3Activity;
import com.fyzk.activity.lottery.gaopin.JSK3Activity;
import com.fyzk.activity.lottery.gaopin.KLSFActivity;
import com.fyzk.activity.lottery.gaopin.NXK3Activity;
import com.fyzk.activity.lottery.gaopin.SD11x5Activity;
import com.fyzk.activity.lottery.gaopin.SSCActivity;
import com.fyzk.activity.lottery.gaopin.XJ11x5Activity;
import com.fyzk.activity.lottery.jc.beidan.BeiDanActivity;
import com.fyzk.activity.lottery.jc.football.newJz.FootballFragmentActivityI;
import com.fyzk.activity.lottery.kaijiang.PrizeListActivity;
import com.fyzk.activity.lottery.shuzicai.DLTActivity;
import com.fyzk.activity.lottery.shuzicai.FC3DActivity;
import com.fyzk.activity.lottery.shuzicai.PL3Activity;
import com.fyzk.activity.lottery.shuzicai.PL5Activity;
import com.fyzk.activity.lottery.shuzicai.QLCActivity;
import com.fyzk.activity.lottery.shuzicai.QXCActivity;
import com.fyzk.activity.lottery.shuzicai.SSQActivity;
import com.fyzk.activity.lottery.shuzicai.klpk.KLPKActivity;
import com.fyzk.activity.lottery.togetherbuy.TogetherBuyHallActivity;
import com.fyzk.activity.lottery.zc_new.OldFootballBQCActivity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballJQCActivity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballR9Activity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballSFCActivity_New;
import com.fyzk.activity.main.GouCaiGridView;
import com.fyzk.activity.main.PromotionActivity;
import com.fyzk.activity.main.PromotionDetailActivity;
import com.fyzk.activity.news.NewsDetailActivity;
import com.fyzk.activity.news.NewsListBean;
import com.fyzk.activity.news.NewsOldActivity;
import com.fyzk.activity.share.ShareCodeActivity;
import com.fyzk.activity.user.userinfo.AuthenticationActivity;
import com.fyzk.adapter.ArticleAdapter;
import com.fyzk.base.BaseFragment;
import com.fyzk.bean.LotteryLayoutItem;
import com.fyzk.bean.UpdateObj;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.net.Commands;
import com.fyzk.utils.AppUtil;
import com.fyzk.utils.L;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LogUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.utils.Utils;
import com.fyzk.view.AutoScrollTextView;
import com.fyzk.view.LotteryItemView;
import com.fyzk.view.MarqueeTextView;
import com.fyzk.view.OnRefreshListener;
import com.fyzk.view.PullToRefreshLayout;
import com.fyzk.view.pagercycle.ADInfo;
import com.fyzk.view.pagercycle.ZJInfo;
import com.tyjc.lottery.R;
import com.wf.domain.UrlDef;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.json.JSONObject;
import org.xutils.common.Callback.CommonCallback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import http.Common.CommandUtil;
import http.StringResultBean;

import static com.alibaba.fastjson.JSON.parseArray;

/**
 * 购彩Fragment
 *
 * @author miao
 */
public class GouCaiFragment extends BaseFragment implements OnRefreshListener {

    private String TAG = "GouCaiActivity";
    private TextView hall_togetherbuy, hall_bbs, hall_news, hall_follow, hall_promotion,hall_zhong;
    LotteryAdapter lotteryAdapter;
    Banner banner;
    ArrayList<LotteryLayoutItem> lotteryItemList_11x5 = new ArrayList<>();
    ArrayList<LotteryLayoutItem> lotteryItemList_klsf = new ArrayList<>();
    ArrayList<LotteryLayoutItem> lotteryItemList_k3 = new ArrayList<>();
    ArrayList<LotteryLayoutItem> lotteryItemList_main = new ArrayList<>();
    private List<ADInfo> infos = new ArrayList<>();

    private List<ZJInfo> zjInfos = new ArrayList<>();
    List<LotteryLayoutItem> lotteryLayoutItemList;
    UpdateObj updateobj;
    private final int group11x5 = 0x1111111;
    private final int groupk3 = 0x2222222;
    private final int groupklsf = 0x3333333;
    LotteryLayoutItem info_11x5g;
    LotteryLayoutItem info_k3g;
    LotteryLayoutItem info_klsf;
    private AlertDialog freshDialog;
    LotteryChildAdapter adapter11x5 = null;
    LotteryChildAdapter adapterk3 = null;
    LotteryChildAdapter adapterklsf = null;
    TextSwitcher textSwitcher;
    ImageView mNoticeImage;
    MarqueeTextView mNoticeText;
    AutoScrollTextView mZhongText;

    private AnimationDrawable animationDrawable;

    private PullToRefreshLayout refreshLayout;

    private ListView footListView;
    ArticleAdapter articleAdapter;
    List<String> strings=new ArrayList();

    LinearLayout kaijiang;

    LinearLayout shishibifen;
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
//        strings.add("恭喜用户187****1254山东11选5中奖129元");
//        strings.add("恭喜用户186****0603竞彩足球混合过关中奖1000元");
//        strings.add("恭喜用户151****8391安徽快三中奖120元");
        super.onViewCreated(view, savedInstanceState);
        refreshLayout = view.findViewById(R.id.goucai_refresh);
        refreshLayout.setOnRefreshListener(this);
        View headView = View.inflate(getContext(), R.layout.layout_goucaitop, null);
        View footView = View.inflate(getContext(), R.layout.layout_goucaitop_footer, null);
        footListView = footView.findViewById(R.id.footerUI_listView);
        articleAdapter = new ArticleAdapter(getContext());
        footListView.setAdapter(articleAdapter);

        banner = (Banner) headView.findViewById(R.id.banner);
//        int screenWidth = ScreenUtils.getInstance().getWidth();
//        int height = screenWidth * 240 / 640;
//        banner.setLayoutParams(new LinearLayout.LayoutParams(screenWidth, height));
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
//        banner.setOnBannerListener(onBannerListener);
        final ListView gv = view.findViewById(R.id.goucai_gv);
        gv.addHeaderView(headView);
        //  gv.addFooterView(footView);
        lotteryAdapter = new LotteryAdapter();
        gv.setAdapter(lotteryAdapter);
        gv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
            }
        });
        kaijiang=headView.findViewById(R.id.kaijiang);
        shishibifen=headView.findViewById(R.id.shishibifen);
        hall_togetherbuy = headView.findViewById(R.id.hall_togetherbuy);
        hall_bbs = headView.findViewById(R.id.hall_bbs);
        hall_follow = headView.findViewById(R.id.hall_follow);
        hall_news = headView.findViewById(R.id.hall_news);
        hall_promotion = headView.findViewById(R.id.hall_promotion);
        hall_togetherbuy.setOnClickListener(this);
        hall_bbs.setOnClickListener(this);
        hall_promotion.setOnClickListener(this);
        hall_follow.setOnClickListener(this);
        hall_news.setOnClickListener(this);

        mNoticeImage = headView.findViewById(R.id.homeFragUI_noticeImage);
        mNoticeText = headView.findViewById(R.id.homeFragUI_noticeText);
        mZhongText=headView.findViewById(R.id.textviewzhong);

////        mZhongText.setText(strings.get(0));
//        mZhongText.setList(strings);
//        mZhongText.startScroll();


        footListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                LogUtils.e("-----i-----" + i);

                Intent intent = new Intent(getContext(), NewsDetailActivity.class);
                intent.putExtra("info", articleAdapter.getData().get(i));
                startActivity(intent);
            }
        });

        kaijiang.setOnClickListener(this);
        shishibifen.setOnClickListener(this);
    }

    /**
     * 通过XML添加帧动画方法一
     */
    private void setXmlFrameAnim() {
        // 把动画资源设置为imageView的背景,也可直接在XML里面设置
        mNoticeImage.setBackgroundResource(R.drawable.notice_frame_anim);
        animationDrawable = (AnimationDrawable) mNoticeImage.getBackground();
    }

    /**
     * 设置公告信息
     *
     * @param content
     */
    public void setNoticeText(String content) {
        if (!TextUtils.isEmpty(content)) {
            mNoticeText.setText(content);
        } else {
            mNoticeText.setText("为响应国家监管信息，请您及时更新自己的身份信息。");
        }
    }

    @Override
    public void onRefresh() {
        if (!AppUtil.isNetworkAvailable(getActivity())) {
            AppToast.getToast().show("网络已断开，请检查网络设置！");
            refreshLayout.refreshFinish(PullToRefreshLayout.REFRESH_FAIL);
            return;
        }
        requestLotteryTypeLayout();
        requestAD();
        requestNewsList();
        getZhongjiang();
    }

    OnBannerListener onBannerListener = new OnBannerListener() {
        @Override
        public void OnBannerClick(int position) {
            Intent intent = new Intent(getContext(), PromotionDetailActivity.class);
            intent.putExtra("news", infos.get(position));
            startActivity(intent);
        }
    };


    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            RequestBuilder<Drawable> thumbnailRequest = Glide
                    .with(getContext())
                    .load(R.drawable.img_top_default);
            Glide.with(getContext()).load(path)
                    .transform(new GlideRoundTransform(getContext(),7))
                    .thumbnail(thumbnailRequest)
                    .into(imageView);
        }

        //提供createImageView 方法，如果不用可以不重写这个方法，主要是方便自定义ImageView的创建
        @Override
        public ImageView createImageView(Context context) {
            return (ImageView) View.inflate(context, R.layout.view_banner, null);
        }
    }

    private void requestLotteryTypeLayout() {
        JSONObject json = new JSONObject();
        httpPostJson(CommandUtil.LOTTERYTYPELAYOUT, Commands.LOTTERYTYPELAYOUT, json, true);
//        try {
//            refreshLayout.refreshFinish(PullToRefreshLayout.REFRESH_SUCCEED);
//        } catch (NullPointerException e) {
//            LogUtil.e(">>>>>>>>>>" + e.getMessage());
//        }
//        lotteryLayoutItemList = parseArray(data, LotteryLayoutItem.class);
//        initLotteryItemViewList();
//        lotteryAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.LOTTERYTYPELAYOUT:
                try {
                    refreshLayout.refreshFinish(PullToRefreshLayout.REFRESH_SUCCEED);
                } catch (NullPointerException e) {
                    LogUtil.e(">>>>>>>>>>" + e.getMessage());
                }
                lotteryLayoutItemList = parseArray(resultBean.getData(), LotteryLayoutItem.class);
                initLotteryItemViewList();
                lotteryAdapter.notifyDataSetChanged();
                break;
            case CommandUtil.GETWINORDER:
                LogUtil.e(">>>>>>>>>>" + result);
                try {
                    JSONObject object = new JSONObject(result);
                    List<ZJInfo> resultBeans = JSON.parseArray(
                            object.getString("data"), ZJInfo.class);
                    zjInfos.clear();
                    zjInfos.addAll(resultBeans);

                    for(int i=0;i<zjInfos.size();i++){
                        String datas="恭喜用户"+zjInfos.get(i).userName+zjInfos.get(i).lotteryType+"中"+zjInfos.get(i).money+"元";
                        strings.add(datas);
                    }
                    mZhongText.setText(strings.get(0));
                    mZhongText.setList(strings);
                    mZhongText.startScroll();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    protected void onSuccess(int requestCode, String result) {
        super.onSuccess(requestCode, result);
        switch (requestCode) {
            case CommandUtil.ARTICLELIST:
                NewsListBean newsList = JSON.parseObject(result, NewsListBean.class);
                String status = newsList.getStatus();
                if (status.equals("200")) {
                    List<NewsListBean.DataBean> dataBeans = newsList.getData();
                    if (dataBeans != null && dataBeans.size() > 0) {
                        articleAdapter.setData(dataBeans);
                        int size = dataBeans.size();
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.dip2px(80 * size) + size * 2);
                        footListView.setLayoutParams(lp);
                    }
                }
                break;
        }
    }

    private void initLotteryItemViewList() {
        lotteryItemList_main.clear();
        lotteryItemList_11x5.clear();
        lotteryItemList_k3.clear();
        lotteryItemList_klsf.clear();
        info_11x5g = null;
        info_k3g = null;
        info_klsf = null;
        StringBuffer currentLotetry = new StringBuffer();
        for (LotteryLayoutItem info : lotteryLayoutItemList) {
            currentLotetry.append(info.getLotteryType());
            currentLotetry.append(",");
            if (AppLotteryInfos.getcurrent_version_lotterys().contains(info
                    .getLotteryType())) {
                if (AppLotteryInfos.getcurrent_version11x5().contains(info
                        .getLotteryType())) {
                    info.setLogoRes(AppLotteryInfos.getLogoId(info.getLotteryType()));
                    lotteryItemList_11x5.add(info);
                    if (info_11x5g == null) {
                        info_11x5g = new LotteryLayoutItem();
                        info_11x5g.setLotteryType(group11x5);
                        info_11x5g.setLogoRes(R.drawable.main_11x5xl);
                        info_11x5g.setLotteryTypeName("11选5系列");
                        info_11x5g.setInSale(1);
                        info_11x5g.setOrderby(info.getOrderby());
                        info_11x5g.setDescription("10分钟一期");
                        lotteryItemList_main.add(info_11x5g);
                    }
                } else if (AppLotteryInfos.getcurrent_versionk3().contains(info
                        .getLotteryType())) {
                    info.setLogoRes(AppLotteryInfos.getLogoId(info.getLotteryType()));
                    lotteryItemList_k3.add(info);
                    if (info_k3g == null) {
                        info_k3g = new LotteryLayoutItem();
                        info_k3g.setLotteryType(groupk3);
                        info_k3g.setLogoRes(R.drawable.main_k3xl);
                        info_k3g.setLotteryTypeName("快三系列");
                        info_k3g.setDescription("好玩易中奖");
                        info_k3g.setInSale(1);
                        info_k3g.setOrderby(info.getOrderby());
                        lotteryItemList_main.add(info_k3g);
                    }
                } else if (AppLotteryInfos.getcurrent_versionklsf().contains(info
                        .getLotteryType())) {
                    info.setLogoRes(AppLotteryInfos.getLogoId(info.getLotteryType()));
                    lotteryItemList_klsf.add(info);
                    if (info_klsf == null) {
                        info_klsf = new LotteryLayoutItem();
                        info_klsf.setLogoRes(R.drawable.main_klsfxl);
                        info_klsf.setLotteryType(groupklsf);
                        info_klsf.setLotteryTypeName("快乐十分系列");
                        info_klsf.setDescription("好玩易中奖");
                        info_klsf.setInSale(1);
                        info_klsf.setOrderby(info.getOrderby());
                        lotteryItemList_main.add(info_klsf);
                    }
                } else {
                    info.setLogoRes(AppLotteryInfos.getLogoId(info.getLotteryType()));
                    lotteryItemList_main.add(info);
                }
            }
        }

        Collections.sort(lotteryItemList_main);

//        LotteryLayoutItem lotteryLayoutItem1 = null;
//        LotteryLayoutItem lotteryLayoutItem2 = null;
//        LotteryLayoutItem lotteryLayoutItem3 = null;
//        LotteryLayoutItem lotteryLayoutItem4 = null;
//        for (int i = 0; i < lotteryItemList_main.size(); i++) {
//            int lotteryId = lotteryItemList_main.get(i).getLotteryType();
//            if (lotteryId == LotteryIDUtils.JZDG_LOTTERY_ID_INT && i != 0) {
//                lotteryLayoutItem1 = lotteryItemList_main.get(i);
//                lotteryItemList_main.remove(i);
//                break;
//            }
//        }
//
//        for (int i = 0; i < lotteryItemList_main.size(); i++) {
//            int lotteryId = lotteryItemList_main.get(i).getLotteryType();
//            if (lotteryId == LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT && i != 0) {
//                lotteryLayoutItem2 = lotteryItemList_main.get(i);
//                lotteryItemList_main.remove(i);
//                break;
//            }
//        }
//
//        for (int i = 0; i < lotteryItemList_main.size(); i++) {
//            int lotteryId = lotteryItemList_main.get(i).getLotteryType();
//            if (lotteryId == LotteryIDUtils.JLDG_LOTTERY_ID_INT && i != 0) {
//                lotteryLayoutItem3 = lotteryItemList_main.get(i);
//                lotteryItemList_main.remove(i);
//                break;
//            }
//        }
//
//        for (int i = 0; i < lotteryItemList_main.size(); i++) {
//            int lotteryId = lotteryItemList_main.get(i).getLotteryType();
//            if (lotteryId == LotteryIDUtils.JLSF_LOTTERY_ID_INT && i != 0) {
//                lotteryLayoutItem4 = lotteryItemList_main.get(i);
//                lotteryItemList_main.remove(i);
//                break;
//            }
//        }
//
//        // 将竞猜足球单关放在第一位
//        if (lotteryLayoutItem1 != null) {
//            lotteryItemList_main.add(0, lotteryLayoutItem1);
//
//            // 将竞猜足球放在第二位
//            if (lotteryLayoutItem2 != null) {
//                lotteryItemList_main.add(1, lotteryLayoutItem2);
//            }
//
//            //讲竞彩篮球单关放在第三位
//            if (lotteryLayoutItem3 != null){
//                lotteryItemList_main.add(2, lotteryLayoutItem3);
//
//                // 将竞猜篮球放在第四位
//                if (lotteryLayoutItem4 != null) {
//                    lotteryItemList_main.add(3, lotteryLayoutItem4);
//                }
//            }else {
//                // 将竞猜篮球放在第三位
//                if (lotteryLayoutItem4 != null) {
//                    lotteryItemList_main.add(2, lotteryLayoutItem4);
//                }
//            }
//        } else {
//            // 将竞猜足球放在第一位
//            if (lotteryLayoutItem2 != null) {
//                lotteryItemList_main.add(0, lotteryLayoutItem2);
//            }
//
//            //将竞彩篮球单关放在第二位
//            if (lotteryLayoutItem3 != null){
//                lotteryItemList_main.add(1, lotteryLayoutItem3);
//
//                // 将竞猜篮球放在第三位
//                if (lotteryLayoutItem4 != null) {
//                    lotteryItemList_main.add(2, lotteryLayoutItem4);
//                }
//            }else {
//                // 将竞猜篮球放在第二位
//                if (lotteryLayoutItem4 != null) {
//                    lotteryItemList_main.add(1, lotteryLayoutItem4);
//                }
//            }
//        }

        if (currentLotetry.length() > 1) {
            currentLotetry.deleteCharAt(currentLotetry.length() - 1);
        }
        MySharedPreferences.getInstance().setCurrentLotterys(currentLotetry.toString());
    }

    @Override
    protected void onFailure(int requestCode, String message) {
        switch (requestCode) {
            case CommandUtil.LOTTERYTYPELAYOUT:
                refreshLayout.refreshFinish(PullToRefreshLayout.REFRESH_FAIL);
                if (freshDialog == null) {
                    freshDialog = new AlertDialog.Builder(getContext()).setMessage("网络异常")
                            .setPositiveButton("刷新",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.cancel();
                                            requestLotteryTypeLayout();
                                        }
                                    }).create();
                }
                freshDialog.show();
                break;
        }
    }

    public View.OnClickListener lotteryClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LotteryItemView itemView = (LotteryItemView) v;
            final int lotteryId = itemView.getLotteryId();
            Intent intent = new Intent();
            switch (lotteryId) {
                case LotteryIDUtils.SSQ_LOTTERY_ID_INT:
                    // 双色球
                    intent.setClass(getContext(), SSQActivity.class);
                    break;
                case LotteryIDUtils.DLT_LOTTERY_ID_INT:
                    // 大乐透
                    intent.setClass(getContext(), DLTActivity.class);
                    break;
                case LotteryIDUtils.QXC_LOTTERY_ID_INT:
                    // 七星彩
                    intent.setClass(getContext(), QXCActivity.class);
                    break;
                case LotteryIDUtils.JZSPF_LOTTERY_ID_INT:
                case LotteryIDUtils.JZBF_LOTTERY_ID_INT:
                case LotteryIDUtils.JZBQC_LOTTERY_ID_INT:
                case LotteryIDUtils.JZZJQ_LOTTERY_ID_INT:
                case LotteryIDUtils.JZHH_LOTTERY_ID_INT:
                case LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT:
                    // 竞猜足球
                    intent.setClass(getContext(), FootballFragmentActivityI.class);
                    break;
                case LotteryIDUtils.JZDG_LOTTERY_ID_INT:
                    // 竞猜足球单关
                    intent.setClass(getContext(), FootballFragmentActivityI.class);
                    intent.putExtra("isDan", true);
                    break;
                case LotteryIDUtils.ZCSFC_LOTTERY_ID_INT:
                    // 足彩14场（胜负彩）
                    intent.setClass(getContext(), OldFootballSFCActivity_New.class);
                    break;
                case LotteryIDUtils.ZCRXJ_LOTTERY_ID_INT:
                    // 足彩任选九
                    intent.setClass(getContext(), OldFootballR9Activity_New.class);
                    break;
                case LotteryIDUtils.PL3_LOTTERY_ID_INT:
                    // 排列三
                    intent.setClass(getContext(), PL3Activity.class);
                    break;
                case LotteryIDUtils.PL5_LOTTERY_ID_INT:
                    // 排列五
                    intent.setClass(getContext(), PL5Activity.class);
                    break;
                case LotteryIDUtils.FC3D_LOTTERY_ID_INT:
                    // 福彩3D
                    intent.setClass(getContext(), FC3DActivity.class);
                    break;
                case LotteryIDUtils.ZCBQC_LOTTERY_ID_INT:
                    // 足彩半全场
                    intent.setClass(getContext(), OldFootballBQCActivity_New.class);
                    break;
                case LotteryIDUtils.QLC_LOTTERY_ID_INT:
                    // 七乐彩
                    intent.setClass(getContext(), QLCActivity.class);
                    break;
                case LotteryIDUtils.ZCJQC_LOTTERY_ID_INT:
                    // 足彩进球彩
                    intent.setClass(getContext(), OldFootballJQCActivity_New.class);
                    break;
                case LotteryIDUtils.SD11XUAN5_LOTTERY_ID_INT:
                    // 山东11选5
                    intent.setClass(getContext(), SD11x5Activity.class);
                    break;
                case LotteryIDUtils.GD11XUAN5_LOTTERY_ID_INT:
                    // 广东11选5
                    intent.setClass(getContext(), GD11x5Activity.class);
                    break;
                case LotteryIDUtils.XJ11XUAN5_LOTTERY_ID_INT:
                    // 新疆11选5
                    intent.setClass(getContext(), XJ11x5Activity.class);
                    break;
                case LotteryIDUtils.CQSSC_LOTTERY_ID_INT:
                    // 重庆时时彩
                    intent.setClass(getContext(), SSCActivity.class);
                    break;
                case LotteryIDUtils.NXK3_LOTTERY_ID_INT:
                    // 宁夏快三
                    intent.setClass(getContext(), NXK3Activity.class);
                    break;
                case LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT:
                    // 安徽快三
                    intent.setClass(getContext(), AnHuiK3Activity.class);
                    break;
                case LotteryIDUtils.JSK3_LOTTERY_ID_INT:
                    // 江苏快三
                    intent.setClass(getContext(), JSK3Activity.class);
                    break;
                case LotteryIDUtils.JLK3_LOTTERY_ID_INT:
                    // 吉林快三
                    intent.setClass(getContext(), JLK3Activity.class);
                    break;
                case LotteryIDUtils.JLSF_LOTTERY_ID_INT:
                    // 竞彩蓝球
                    intent.setClass(getContext(), JCLQFragmentActivityI.class);
                    break;
                case LotteryIDUtils.JLDG_LOTTERY_ID_INT:
                    // 竞彩蓝球单关
                    intent.setClass(getContext(), JCLQFragmentActivityI.class);
                    intent.putExtra("isDan", true);
                    break;
                case LotteryIDUtils.KLSF_LOTTERY_ID_INT:
                    // 重庆快乐十分
                    intent.setClass(getContext(), KLSFActivity.class);
                    break;
                case LotteryIDUtils.GDKLSF_LOTTERY_ID_INT:
                    // 广东快乐十分
                    intent.setClass(getContext(), GDKLSFActivity.class);
                    break;
                case LotteryIDUtils.KLPK_LOTTERY_ID_INT:
                    // 快乐扑克
                    intent.setClass(getContext(), KLPKActivity.class);
                    break;
                case LotteryIDUtils.GXK3_LOTTERY_ID_INT:
                    // 广西快三
                    intent.setClass(getContext(), GXK3Activity.class);
                    break;
                case LotteryIDUtils.DCSPF_LOTTERY_ID_INT:
                    // 单场
                    intent.setClass(getContext(), BeiDanActivity.class);
                    break;
            }
            getContext().startActivity(intent);
        }
    };

    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.hall_togetherbuy:
                // 合买
                intent.setClass(getContext(), TogetherBuyHallActivity.class);
                break;
            case R.id.hall_bbs:
                // 论坛
                intent.setClass(getContext(), ForumActivity.class);
                break;
            case R.id.hall_follow:
                // 开奖公告
                intent.setClass(getContext(), PrizeListActivity.class);
                break;
            case R.id.kaijiang:
                // 开奖公告
                startActivity(new Intent(getActivity(), PrizeListActivity.class));
                break;
            case R.id.shishibifen:
                //实时比分
//                startActivity(new Intent(getActivity(), ShishibifenActivity.class));
                startActivity(new Intent(getActivity(), RealtimeScoreActivity.class));
                intent.putExtra("id", 1);
                break;
            case R.id.hall_promotion:
                // 活动公告
                intent.setClass(getContext(), PromotionActivity.class);
                break;
            case R.id.hall_news:
                // 资讯
                intent.setClass(getContext(), NewsOldActivity.class);
                break;
        }
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            L.e(TAG, e.getMessage());
        }
    }



    @Override
    public void onResume() {
        super.onResume();
        if (infos.size() == 0) {
            requestAD();
        }
        if (lotteryLayoutItemList == null) {
            requestLotteryTypeLayout();
        }
        if (animationDrawable != null && !animationDrawable.isRunning()) {
            animationDrawable.start();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (animationDrawable != null && animationDrawable.isRunning()) {
            animationDrawable.stop();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        requestAD();
        requestNewsList();
        getZhongjiang();
        return inflater.inflate(R.layout.ac_goucai, null);
    }

    private void requestAD() {
        RequestParams params = new RequestParams(UrlDef.NEWS_URL + Commands.NEWSLIST);
        params.addBodyParameter("category", "luotuo_banner");
//        params.addBodyParameter("page", "1");
//        params.addBodyParameter("perpage", "5");
        params.addBodyParameter("src", App.getChannel());
        sendRequest(HttpMethod.GET, params);
    }

    class LotteryChildAdapter extends BaseAdapter {
        private ArrayList<LotteryLayoutItem> lotteryLayoutItems;

        public LotteryChildAdapter(ArrayList<LotteryLayoutItem> lotteryItemViews) {
            this.lotteryLayoutItems = lotteryItemViews;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup arg2) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(getContext(), R.layout.item_lotteryitemview, null);
                holder.itemView = (LotteryItemView) convertView;
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.itemView.setLotteryInfo(lotteryLayoutItems.get(position));
            holder.itemView.setOnClickListener(lotteryClickListener);
            return convertView;
        }

        class ViewHolder {
            LotteryItemView itemView;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public int getCount() {
            return lotteryLayoutItems.size();
        }
    }

    private Map<Integer, GridTag> visible = new HashMap<>();

    class GridTag {
        boolean visible_left;
        boolean visible_right;
        ArrayList<LotteryLayoutItem> items_left;
        ArrayList<LotteryLayoutItem> items_right;
    }

    class LotteryAdapter extends BaseAdapter {
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(getContext(), R.layout.item_goucai, null);
                holder.left = (LotteryItemView) convertView.findViewById(R.id.item_goucai_left);
                holder.right = (LotteryItemView) convertView.findViewById(R.id.item_goucai_right);
                holder.grid_left = (GouCaiGridView) convertView.findViewById(R.id
                        .item_goucai_grid_left);
                holder.grid_right = (GouCaiGridView) convertView.findViewById(R.id
                        .item_goucai_grid_right);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (visible.get(position) == null) {
                GridTag tag = new GridTag();
                tag.visible_left = false;
                tag.visible_right = false;
                holder.grid_left.setVisibility(View.GONE);
                holder.grid_right.setVisibility(View.GONE);
                visible.put(position, tag);
            } else {
                GridTag tag = visible.get(position);
                if (tag.visible_left) {
                    holder.grid_left.setVisibility(View.VISIBLE);
                    LotteryChildAdapter adapter = new LotteryChildAdapter(tag.items_left);
                    holder.grid_left.setAdapter(adapter);
                    holder.grid_right.setVisibility(View.GONE);
                } else {
                    holder.grid_left.setVisibility(View.GONE);
                }
                if (tag.visible_right) {
                    holder.grid_right.setVisibility(View.VISIBLE);
                    LotteryChildAdapter adapter = new LotteryChildAdapter(tag.items_right);
                    holder.grid_right.setAdapter(adapter);
                    holder.grid_left.setVisibility(View.GONE);
                } else {
                    holder.grid_right.setVisibility(View.GONE);
                }
            }
            final int left_index = (position) * 2;
            final int right_index = (position) * 2 + 1;
            LotteryLayoutItem leftLayout = lotteryItemList_main.get(left_index);
            holder.left.setLotteryInfo(leftLayout);
            holder.left.setTag(R.string.home_left_grid, holder.grid_left);
            holder.left.setTag(R.string.home_right_grid, holder.grid_right);
            if (leftLayout.getLotteryType() != groupk3 && leftLayout.getLotteryType() !=
                    groupklsf && leftLayout.getLotteryType() != group11x5) {
                holder.left.setOnClickListener(lotteryClickListener);
            } else {
                holder.left.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        GouCaiGridView gridView = (GouCaiGridView) v.getTag(R.string
                                .home_left_grid);
                        GouCaiGridView gridView_right = (GouCaiGridView) v.getTag(R.string
                                .home_right_grid);
                        if (gridView.getVisibility() == View.GONE) {
                            gridView.setVisibility(View.VISIBLE);
                            gridView_right.setVisibility(View.GONE);
                            GridTag tag = visible.get(position);
                            tag.visible_left = true;
                            tag.visible_right = false;
                            initGridView(left_index, gridView, tag);
                        } else {
                            gridView.setVisibility(View.GONE);
                            GridTag tag = visible.get(position);
                            tag.visible_left = false;
                        }
                    }
                });
            }
            if (right_index < lotteryItemList_main.size()) {
                LotteryLayoutItem rightLayout = lotteryItemList_main.get(right_index);
                holder.right.setLotteryInfo(rightLayout);
                holder.right.setVisibility(View.VISIBLE);
                holder.right.setTag(R.string.home_left_grid, holder.grid_left);
                holder.right.setTag(R.string.home_right_grid, holder.grid_right);
                if (rightLayout.getLotteryType() != groupk3 && rightLayout.getLotteryType() !=
                        groupklsf && rightLayout.getLotteryType() != group11x5) {
                    holder.right.setOnClickListener(lotteryClickListener);
                } else {
                    holder.right.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            GouCaiGridView gridView_left = (GouCaiGridView) v.getTag(R.string
                                    .home_left_grid);
                            GouCaiGridView gridView = (GouCaiGridView) v.getTag(R.string
                                    .home_right_grid);
                            if (gridView.getVisibility() == View.GONE) {
                                gridView.setVisibility(View.VISIBLE);
                                gridView_left.setVisibility(View.GONE);
                                GridTag tag = visible.get(position);
                                tag.visible_right = true;
                                tag.visible_left = false;
                                initGridView(right_index, gridView, tag);
                            } else {
                                gridView.setVisibility(View.GONE);
                                GridTag tag = visible.get(position);
                                tag.visible_right = false;
                            }
                        }
                    });
                }
            } else {
                holder.right.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

        @Override
        public LotteryLayoutItem getItem(int position) {
            return null;
        }

        @Override
        public int getCount() {
            int count = lotteryItemList_main.size();
            if (count % 2 == 0) {
                return count / 2;
            } else {
                return count / 2 + 1;
            }
        }

        class ViewHolder {
            LotteryItemView left;
            LotteryItemView right;
            GouCaiGridView grid_left;
            GouCaiGridView grid_right;
        }
    }

    private void initGridView(int index, GouCaiGridView gridView, GridTag tag) {
        if (lotteryItemList_main.get(index).getLotteryType() == group11x5) {
            adapter11x5 = new LotteryChildAdapter(lotteryItemList_11x5);
            if (tag.visible_left) {
                tag.items_left = lotteryItemList_11x5;
            } else {
                tag.items_right = lotteryItemList_11x5;
            }
            gridView.setAdapter(adapter11x5);
        } else if (lotteryItemList_main.get(index).getLotteryType() == groupklsf) {
            adapterklsf = new LotteryChildAdapter(lotteryItemList_klsf);
            gridView.setAdapter(adapterklsf);
            if (tag.visible_left) {
                tag.items_left = lotteryItemList_klsf;
            } else {
                tag.items_right = lotteryItemList_klsf;
            }
        } else if (lotteryItemList_main.get(index).getLotteryType() == groupk3) {
            adapterk3 = new LotteryChildAdapter(lotteryItemList_k3);
            if (tag.visible_left) {
                tag.items_left = lotteryItemList_k3;
            } else {
                tag.items_right = lotteryItemList_k3;
            }
            gridView.setAdapter(adapterk3);
        }
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void sendRequest(HttpMethod method,
                            RequestParams params) {

        if (!Utils.isNetworkAvailable(getActivity())) {
            return;
        }

        x.http().request(method, params, new
                CommonCallback<String>() {
                    public void onSuccess(String result) {
                        try {
                            LogUtils.e("====banner====" + result);
                            JSONObject object = new JSONObject(result);
                            List<ADInfo> resultBeans = parseArray(
                                    object.getString("data"), ADInfo.class);
                            infos.clear();
                            infos.addAll(resultBeans);
                            List<String> urls = new ArrayList<>();
                            for (ADInfo info : resultBeans) {
                                urls.add(info.icon_url);
                                infos.add(info);
                            }
                            System.out.println(urls);
                            //设置图片集合
                            banner.setImages(urls);
                            //banner设置方法全部调用完毕时最后调用
                            banner.start();
                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        LogUtils.e("====ex====" + ex.getMessage());
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
    }

    /**
     * 获取新闻列表
     */
    protected void requestNewsList() {
        RequestParams params = new RequestParams(UrlDef.NEWS_URL + Commands.NEWSLIST);
        params.addBodyParameter("category", "jinritoutiao");
        params.addBodyParameter("src", "src1");
        params.addBodyParameter("page", "1");
        params.addBodyParameter("perpage", "4");
        params.addBodyParameter("iscontent", "0");
        sendRequestGet(HttpMethod.GET, CommandUtil.ARTICLELIST, params);
    }

    /**
     * 获取中奖信息
     */
    protected void getZhongjiang() {
//        RequestParams params = new RequestParams(UrlDef.getZhongjiang );
//        sendRequestsGet(HttpMethod.GET, params);
        httpPostJson(CommandUtil.GETWINORDER, "getWinOrder", new JSONObject(), true);
    }


    public void sendRequestsGet(HttpMethod method, RequestParams params) {
        x.http().request(method, params, new
                CommonCallback<String>() {
                    public void onSuccess(String result) {
                        try {
                            JSONObject object = new JSONObject(result);
                            List<ZJInfo> resultBeans = JSON.parseArray(
                                    object.getString("data"), ZJInfo.class);
                            zjInfos.clear();
                            zjInfos.addAll(resultBeans);

                            for(int i=0;i<zjInfos.size();i++){
                                String datas="恭喜用户"+zjInfos.get(i).userName+zjInfos.get(i).lotteryType+"中"+zjInfos.get(i).money+"元";
                                strings.add(datas);
                            }

                        } catch (Exception e) {

                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
    }
}
